package com.dlc.shop.mp.common.api.controller;

import com.dlc.shop.common.exception.YamiShopBindException;
import com.dlc.shop.common.response.ServerResponseEntity;
import com.dlc.shop.config.WxConfig;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * jsapi 签名
 * @author yami
 */
@RestController
@RequestMapping("/p/wx/jsapi")
@Tag(name = "微信jsapi接口")
@AllArgsConstructor
public class JsapiController {

    private final WxConfig wxConfig;

    @PostMapping("/createJsapiSignature")
    @Operation(summary = "生成jsapi签名" , description = "根据url生成当前页面的jsapi签名")
    @Parameter(name = "url", description = "url" , required = true)
    public ServerResponseEntity<WxJsapiSignature> createJsapiSignature(@RequestBody String url) {
        WxJsapiSignature jsapiSignature;
        try {
            jsapiSignature = wxConfig.getWxMpService().createJsapiSignature(url);
        } catch (WxErrorException e) {
            throw new YamiShopBindException(e.getError().toString());
        }
        return ServerResponseEntity.success(jsapiSignature);
    }
}
